﻿using Mengsk.ProxyDelivery.Domain;
using Mengsk.ProxyDelivery.Print.Domain;
using Mengsk.ProxyDelivery.Print.Service;
using Mengsk.ProxyDelivery.Service;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;

namespace Mengsk.ProxyDelivery.Print
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        public static readonly string DIR = new FileInfo(Assembly.GetExecutingAssembly().Location).DirectoryName;
        public static readonly string DIR_CONFIG = DIR + "\\Config\\";
        public static readonly string DIR_DATA = DIR + "\\Data\\";

        public static Operator LoginOperator { get; set; }

        public App()
        {
            Directory.CreateDirectory(DIR_CONFIG);
            Directory.CreateDirectory(DIR_DATA);
        }

        public static void DoEvents()
        {
            Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, new Action(delegate { }));
        }

        protected override void OnStartup(StartupEventArgs e)
        {
            try
            {
                ServiceContainer.ServerAddress = LocalConfigService.GetValue("ServerAddress", "http://www.76df.com:8080/proxydelivery");
                Mengsk.ProxyDelivery.UI.LoginWindow lw = new ProxyDelivery.UI.LoginWindow();
                bool? ret = lw.ShowDialog();
                if (ret == null || ret.Value == false)
                {
                    this.Shutdown();
                    return;
                }
                LoginOperator = lw.LoginUser;
                new MainWindow().ShowDialog();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "应用程序错误", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            finally
            {
                this.Shutdown();
            }
        }
    }
}
